package com.lv.suyiyong.hfrwork.adapter

import android.graphics.Color
import android.view.View
import android.widget.FrameLayout
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.DraggableModule
import com.lv.common.imp.OnPositionTListener
import com.lv.common.widget.recyclerview.DragTouchHolder
import com.lv.service.bean.TVIVItemBean
import com.lv.suyiyong.hfrwork.R
import com.lv.suyiyong.hfrwork.databinding.ItemFactoryLayoutBinding

/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class ManagerFactoryAdapter(
    private val datas: MutableList<TVIVItemBean>, private var mExpird: String = "0",
    private var height: Int = 132
) : BaseQuickAdapter<TVIVItemBean, DragTouchHolder>
    (R.layout.item_factory_layout, datas), DraggableModule {

    override fun convert(holder: DragTouchHolder, item: TVIVItemBean) {
        //禁止recyclerview复用
//        holder.setIsRecyclable(false)
        val mBinding: ItemFactoryLayoutBinding = holder.getBind() as ItemFactoryLayoutBinding
//        mBinding.itemRoot = item
        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutPosition = holder.layoutPosition
        val layoutRecyclerParams = mBinding.clParent.layoutParams as FrameLayout.LayoutParams
        layoutRecyclerParams.height = SizeUtils.dp2px((height + 10).toFloat())
        mBinding.clParent.layoutParams = layoutRecyclerParams
        val layoutParams = mBinding.ivImg.layoutParams as FrameLayout.LayoutParams
        layoutParams.height = SizeUtils.dp2px(height.toFloat())
        mBinding.ivImg.layoutParams = layoutParams
        val layoutVideoParams = mBinding.ivImgVideo.layoutParams as FrameLayout.LayoutParams
        layoutVideoParams.height = SizeUtils.dp2px(height.toFloat())
        mBinding.ivImgVideo.layoutParams = layoutVideoParams
        val type = item.type
        mBinding.ivImg.visibility = if (type == "1") View.GONE else View.VISIBLE
        mBinding.ivImgVideo.visibility = if (type == "1") View.VISIBLE else View.GONE
        mBinding.ivImgVideo.adjustViewBounds = true
        mBinding.ivImgVideo.setBackgroundColor(Color.parseColor("#000000"))
//        val border: String? = item.border
//        if (!border.isNullOrEmpty()) {
//            val jsonType = object : TypeToken<FactoryBorderItemBean>() {}.type
//            val factoryBorderItemBean: FactoryBorderItemBean =
//                GsonUtils.fromJson(border, jsonType)
//            if (factoryBorderItemBean == null || factoryBorderItemBean.id.isNullOrEmpty()) {
//                mBinding.ivTag.visibility = View.GONE
//                mBinding.ivBorder.visibility = View.GONE
////                mBinding.viewShadow.visibility = View.VISIBLE
////                if (mExpird == "1") {
////                    mBinding.addBroder.visibility = View.VISIBLE
////                } else {
////                    mBinding.addBroder.visibility = View.GONE
////                }
//            } else {//显示
//                mBinding.ivBorder.visibility = View.VISIBLE
//                val show = factoryBorderItemBean.show ?: "1"
//                var width = factoryBorderItemBean.wide
//                if (width.isNullOrEmpty()) {
//                    width = "0"
//                }
//                val k = width.toFloat() / (735.0f)
//                Glide.with(context).load(factoryBorderItemBean.image).override(
//                    SizeUtils.dp2px(236.0f),
//                    SizeUtils.dp2px(height.toFloat())
//                ).into(mBinding.ivBorder)
////                Glide.with(context).load(factoryBorderItemBean.image).override(
////                    SizeUtils.dp2px(236.0f),
////                    SizeUtils.dp2px(height.toFloat())
////                ).into(mBinding.ivBroderImg)
//
////                mBinding.ivBorder.load(factoryBorderItemBean.image)
////                val borderRadius = factoryBorderItemBean.borderRadius ?: "0"
////                val radius = borderRadius.toFloat() * k
//
//                if (show == "1") {
//                    val mWidth = k * 200.0f
//                    mBinding.ivTag.visibility = View.VISIBLE
//                    val tagParams = mBinding.ivTag.layoutParams as ConstraintLayout.LayoutParams
//                    tagParams.width = SizeUtils.dp2px(mWidth)
//                    mBinding.ivTag.layoutParams = tagParams
//                    mBinding.ivTag.LoadSrc(factoryBorderItemBean.placard)
//                } else {
//                    mBinding.ivTag.visibility = View.GONE
//                }
//                val position = factoryBorderItemBean.position
//                mBinding.viewSubLeft.visibility =
//                    if (position == "centre") View.VISIBLE else View.GONE
//
////                mBinding.viewShadow.visibility = View.GONE
////                mBinding.addBroder.visibility = View.GONE
//            }
//        } else {//不显示
//            mBinding.ivTag.visibility = View.GONE
////            mBinding.viewBorder.visibility = View.GONE
////            mBinding.viewShadow.visibility = View.VISIBLE
////            if (mExpird == "1") {
////                mBinding.addBroder.visibility = View.VISIBLE
////            } else {
////                mBinding.addBroder.visibility = View.GONE
////            }
//        }

        //0 图片 1 视频
        mBinding.ivPlay.visibility = if (item.type == "0") View.GONE else View.VISIBLE
        Glide.with(context).load(item.text)
            .override(SizeUtils.dp2px(236.0f), SizeUtils.dp2px(height.toFloat()))
            .into(mBinding.ivImg)
        Glide.with(context).load(item.text)
            .override(SizeUtils.dp2px(236.0f), SizeUtils.dp2px(height.toFloat()))
            .into(mBinding.ivImgVideo)
        mBinding.ivClear.setOnClickListener {
            onViewDeleteClickListener?.onClick(layoutPosition, item)
        }
//        viewShadow.setOnClickListener {
//            onViewClickListener?.onClick(layoutPosition, item)
//        }
        mBinding.clParent.setOnClickListener {
            onViewClickListener?.onClick(layoutPosition, item)
        }

    }

    fun setHeight(h: Int) {
        height = h
        notifyDataSetChanged()
    }

    var onViewDeleteClickListener: OnPositionTListener<TVIVItemBean>? = null

    //    var onViewBorderClickListener: OnPositionTListener<TVIVItemBean>? = null
    var onViewClickListener: OnPositionTListener<TVIVItemBean>? = null


}